package com.cnswhy.cloud.server.system.controller;

import cn.hutool.core.util.ArrayUtil;
import com.cnswhy.cloud.common.core.base.BaseEnum;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.server.common.entity.enumeration.*;
import com.cnswhy.cloud.server.system.enumeration.AuthorizeType;
import com.cnswhy.cloud.server.system.enumeration.DataScopeType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 通用 控制器
 *
 * @author zuihou
 * @date 2019/07/25
 */
@Slf4j
@RestController
@RefreshScope
@Api(value = "Common", tags = "通用Controller")
public class OauthGeneralController {
    private final static Map<String, Map<String, String>> ENUM_MAP = new HashMap<>(8);

    static {
        ENUM_MAP.put(AuthorizeType.class.getSimpleName(), BaseEnum.getMap(AuthorizeType.values()));
        ENUM_MAP.put(ConfigKey.class.getSimpleName(), BaseEnum.getMap(ConfigKey.values()));
        ENUM_MAP.put(TransType.class.getSimpleName(), BaseEnum.getMap(TransType.values()));
        ENUM_MAP.put(WalletType.class.getSimpleName(), BaseEnum.getMap(WalletType.values()));
        ENUM_MAP.put(WithdrawType.class.getSimpleName(), BaseEnum.getMap(WithdrawType.values()));
        ENUM_MAP.put(DataScopeType.class.getSimpleName(), BaseEnum.getMap(DataScopeType.values()));
        ENUM_MAP.put(ExpressCompany.class.getSimpleName(), BaseEnum.getMap(ExpressCompany.values()));
        ENUM_MAP.put(BusType.class.getSimpleName(), BaseEnum.getMap(BusType.values()));
        ENUM_MAP.put(SplitType.class.getSimpleName(), BaseEnum.getMap(SplitType.values()));
        ENUM_MAP.put(IotType.class.getSimpleName(), BaseEnum.getMap(IotType.values()));
        ENUM_MAP.put(OrderReportType.class.getSimpleName(), BaseEnum.getMap(OrderReportType.values()));
    }
    @ApiOperation(value = "获取当前系统指定枚举", notes = "获取当前系统指定枚举")
    @GetMapping("/enums")
    public R<Map<String, Map<String, String>>> enums(@RequestParam(value = "codes[]", required = false) String[] codes) {
        if (ArrayUtil.isEmpty(codes)) {
            return R.success(ENUM_MAP);
        }

        Map<String, Map<String, String>> map = new HashMap<>(codes.length);

        for (String code : codes) {
            if (ENUM_MAP.containsKey(code)) {
                map.put(code, ENUM_MAP.get(code));
            }
        }
        return R.success(map);
    }
}
